package com.dzh.dao;

import com.dzh.pojo.Department;
import com.dzh.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    //员工所属的部门
    @Autowired
    private DepartmentDao departmentlDao;
    static {
        employees = new HashMap<Integer,Employee>();
        employees.put(101,new Employee(1001,"AA","123456@qq.com",0,new Department(101,"教学部")));
        employees.put(101,new Employee(1002,"BB","123456@qq.com",1,new Department(102,"市场部")));
        employees.put(101,new Employee(1003,"CC","123456@qq.com",0,new Department(103,"后勤部")));
        employees.put(101,new Employee(1004,"DD","123456@qq.com",1,new Department(104,"运营部")));
        employees.put(101,new Employee(1005,"EE","123456@qq.com",0,new Department(105,"小卖部")));
    }
    //主键自增
    private static Integer initId = 1006;
    //增加一个员工
    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        //通过输入一个值然后查询这个部门是什么，然后在set进去
        employee.setDepartment(departmentlDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }
    //查询全部员工的信息
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //查询某个员工
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    //删除员工
    public void delete(Integer id){
        employees.remove(id);
    }
}
